//
//  FYComposeTextView.swift
//  Mircore
//
//  Created by apple on 16/8/16.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

//自定义UITextView

//@IBDesignable  实时看到修改后属性的值
@IBDesignable
class FYComposeTextView: UITextView {

   //占位label
    
    private  lazy  var  placeHolderLabel: UILabel = {
    
        let  label  = UILabel()
        
        label.font  = UIFont.systemFontOfSize(12)
        
        label.text  = "听说下雨很适合情侣哟"
        
        label.textColor  = UIColor.lightGrayColor()
    
        //多行显示
        
        label.numberOfLines  = 0
        
        return  label
    
    }()
    
    //提供外界设置占位文字的属性
    
    //@IBInspectable  给xib/sb添加可以设置的属性
    
    @IBInspectable var  placeHolder: String?  {
    
        didSet  {
        
        
          placeHolderLabel.text  = placeHolder
        
        }
    
    }
    
    //重写font
    
    override  var  font: UIFont? {
    
        didSet {
        
            if  font  != nil {
            
              //当前外置设置Font让占位label与其同步
                
                placeHolderLabel.font  = font
            
            }
        
        }
    
    }
    
    
    //重写text
    
    override  var   text: String? {
    
        didSet  {
        
        //根据文本内容显示占位label
            
            placeHolderLabel.hidden  = hasText()
        
        }
     }
    
    //手写代码方式

    
    override  init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
      super.init(coder: aDecoder)
        
        setupUI()
    }
    
    
    //添加控件设置约束
    
    private  func  setupUI() {
    
    
    //监听文字改变的通知
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FYComposeTextView.textChange), name: UITextViewTextDidChangeNotification, object: nil)
    
      
        
        addSubview(placeHolderLabel)
        
        
        //使用系统约束
        
        placeHolderLabel.translatesAutoresizingMaskIntoConstraints  = false
        
        addConstraint(NSLayoutConstraint(item: placeHolderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
    }
    
    //MARK:-监听文字改变的通知
    
    @objc  private  func  textChange()  {
    
//        if  hasText()  {
//        
//         placeHolderLabel.hidden  = true
//        }else  {
//        
//        placeHolderLabel.hidden  = false
//        }
    
        //根据是否有值判断是否显示占位label
        
        placeHolderLabel.hidden  = hasText()
    
    }
    
    //给子控件设置约束
    
    override func  layoutSubviews() {
        
        super.layoutSubviews()
        
        //设置x  y 坐标
        
        placeHolderLabel.frame.origin.x  = 5
        
        placeHolderLabel.frame.origin.y = 7
    }
    
    deinit {
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
